package vy.org.app.common;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.vy.dal.dao.views.VyEmpModuleGroupDAO;
import org.vy.dal.dao.views.VyEmpModuleGroupDO;

public class UserCtx {
	
	private List<String> modules = new ArrayList<String>();
	private String id = null;
	
	UserCtx(HttpServletRequest req) throws SessionExpiredException, Exception {
		HttpSession session = req.getSession();
		id = (String)session.getAttribute(AppConstants.SESSION_USER_ID);
		if(id == null || "".equals(id)){
			throw new SessionExpiredException();
		}
		List<VyEmpModuleGroupDO> moduleLst = VyEmpModuleGroupDAO.findByVyId(id);
		
		if(moduleLst != null && !moduleLst.isEmpty()) {
			for(VyEmpModuleGroupDO mgDO : moduleLst){
				modules.add(mgDO.getModuleId());
			}
		}
	}
	
	public String getId() {
		return id;
	}

	public boolean hasAccessToModule(String name){
		return modules.contains(name);
	}
}
